﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using r4u.data.access.layer.REGISTER;

namespace r4u.business.logic.layer.REGISTER
{
    public class User
    {
        public string name { get; set; }
        public string email { get; set; }
        public string password { get; set; }
        public string verify { get; set; }
        public string company { get; set; }
        public string address1 { get; set; }
        public string address2 { get; set; }
        public string city { get; set; }
        public string State { get; set; }
        public string country { get; set; }
        public string postalcode { get; set; }
        public string phone { get; set; }
        public string alert1 { get; set; }
        public string alert2 { get; set; }

        public bool InsertUser(User user)
        {
            UserDAO userDao = new UserDAO();

            if (userDao.CheckExist(user.email))
            {
                return false;
            }
            else
            {
                return userDao.InsertUser(user.name, user.company, user.address1, user.address2, user.postalcode, user.city, user.State, user.country, user.email, Int32.Parse(user.phone), user.alert1, user.alert2, user.password);
            }
        }

        public bool CheckUser(Login login)
        {
            UserDAO userDao = new UserDAO();
            return userDao.CheckUser(login.email, login.password);
        }


        public Contact[] GetContact(string email)
        {
            UserDAO userDao = new UserDAO();
            Contact[] contacts = new Contact[2];
            contacts[0] = new Contact("EMAIL", String.Format("Via Email ({0})", email));
            contacts[1] = new Contact("PHONE", String.Format("Via SMS (+{0})", userDao.GetPhone(email)));

            return contacts;
        }


        public bool ActivateAccount(string email, string code)
        {
            UserDAO userDao = new UserDAO();

            if (userDao.CheckActivationCode(email, code))
            {
                return userDao.UpdateStatus(email, "VERIFIED");
            }
            else
            {
                return false;
            }
        }

        public bool ResetPassword(string email, string password)
        {
            UserDAO userDao = new UserDAO();
            return userDao.ResetPassword(email, password);
        }

        public string GetStatus(string email)
        {
            UserDAO userDao = new UserDAO();
            return userDao.GetStatus(email);
        }


        public bool ActivateTrial(string email)
        {
            UserDAO userDao = new UserDAO();
            return userDao.ActivateAccount(email, DateTime.Now, DateTime.Now.AddDays(30), "TRIAL");
        }


        public bool ActivateMonthlySubscription(string email)
        {
            UserDAO userDao = new UserDAO();
            return userDao.ActivateAccount(email, DateTime.Now, DateTime.Now.AddDays(30), "PREMIUM");
        }

        public bool ActivateAnnualSubscription(string email)
        {
            UserDAO userDao = new UserDAO();
            return userDao.ActivateAccount(email, DateTime.Now, DateTime.Now.AddDays(365), "PREMIUM");
        }


        public string GetEmailBySecurityCode(string code)
        {
            UserDAO userDao = new UserDAO();
            return userDao.GetEmailBySecurityCode(code);
        }


    }
}
